import 'package:flutter/material.dart';
import 'pages/bars.dart';
import 'pages/search.dart';
import 'pages/news.dart';
import 'pages/form.dart';
import 'pages/shop.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  // 1.配置路由
  final Map routes = {
    "/": (context) => const Bars(),
    "/news": (context) => const NewsPage(),
    "/search": (context) => const SearchPage(),
    "/form": (context, {arguments}) => FormPage(arguments: arguments),
    "/shop": (context, {arguments}) => ShopPage(arguments: arguments)
  };

  MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      // home: Bars(),
      debugShowCheckedModeBanner: false,
      initialRoute: "/",
      // 2.配置onGenerateRoute 固定写法
      onGenerateRoute: (RouteSettings settings) {
        // print(settings);
        // print(settings.name);
        // print(settings.arguments);
        // 统一处理
        final String? name = settings.name; // /news 或者 /search
        final Function? pageContentBuilder =
            routes[name]; // Function (context) => const NewsPage()
        if (pageContentBuilder != null) {
          if (settings.arguments != null) {
            final Route route = MaterialPageRoute(
                builder: (context) =>
                    pageContentBuilder(context, arguments: settings.arguments));
            return route;
          } else {
            final Route route = MaterialPageRoute(
                builder: (context) => pageContentBuilder(context));
            return route;
          }
        }
        return null;
      },
    );
  }
}
